package com.tevintao.marketingsystem.tool;

import android.text.TextUtils;
import android.util.Log;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.shell.Shell;

/**
 * Created by tevintao on 2016/1/18.
 */
public class SystemPathTool {

    public static boolean exitPath(String absolutePath) {
        String cmd = "cd " + absolutePath;
        Shell.ShellCommand command = Shell.runAsRoot(cmd);

        if (command.output != null && command.output.size() <= 0) {
            return true;
        } else {
            return false;
        }
    }

    public static String getOutPutContent(String fileAbsolutePath) {
        String cmd = "cat " + fileAbsolutePath;
        Shell.ShellCommand command = Shell.runAsRoot(cmd);
        Log.i("SystemPathTool", command.toString());
        if (command.output != null && command.output.size() <= 0) {
            return "";
        } else {
            return command.output.get(0).toString();
        }
    }

    public static String getOutPutContentNoRoot(String fileAbsolutePath) {
        String cmd = "cat " + fileAbsolutePath;
        Shell.ShellCommand command = Shell.run(cmd);
        Log.i("SystemPathTool", command.toString());
        if (command.output != null && command.output.size() <= 0) {
            return "";
        } else {
            return command.output.get(0).toString();
        }
    }

    public static boolean exitFile(String absolutePath) {
        String cmd = "ls " + absolutePath;
        Shell.ShellCommand command = Shell.runAsRoot(cmd);
        if (command.output != null && command.output.size() == 1 && command.output.get(0).contains("No") && command.output.get(0).contains("file")) {
            return false;
        } if (command.output != null && command.output.size() == 1 && command.output.get(0).contains("Not") && command.output.get(0).contains("directory")) {
            ///data/data/account_cache/localdev.txt/device.txt: Not a directory
            return false;
        } else {
            return true;
        }
    }



    public static boolean copy(String srcAbsolutePath, String targetAbsolutePath) {
        String cmd = "cp -R -a " + srcAbsolutePath + " " + targetAbsolutePath;
        LogManager.getInstance().logInfo("copyDirect cmd = " + cmd);
        Shell.ShellCommand command = Shell.runAsRoot(cmd);
        LogManager.getInstance().logInfo(cmd + " result = " + command.output);
        if (command.output != null && command.output.size() <= 0) {
            return true;
        } else {
            return false;
        }
    }


    public static boolean remove(String removeAbsolutePath) {
        String cmd = "rm -rf " + removeAbsolutePath;
        Shell.ShellCommand command = Shell.runAsRoot(cmd);
        if (command.output != null && command.output.size() <= 0) {
            return true;
        } else {
            return false;
        }
    }


    public static boolean Link(String srcAbsolutePath, String linkPath) {
        String cmd = "ln -s " + srcAbsolutePath + " " + linkPath;
        Shell.ShellCommand command = Shell.runAsRoot(cmd);
        if (command.output != null && command.output.size() <= 0) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean mkDir(String srcAbsolutePath) {
        String cmd = "busybox mkdir -p " + srcAbsolutePath;
        Shell.ShellCommand command = Shell.runAsRoot(cmd);
        if (command.output != null && command.output.size() <= 0) {
            return true;
        } else {
            return false;
        }
    }


    public static boolean mkD(String srcAbsolutePath) {
        String cmd = "mkdir -p " + srcAbsolutePath;
        Shell.ShellCommand command = Shell.runAsRoot(cmd);
        if (command.output != null && command.output.size() <= 0) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean noResultIsSuccess(String cmd) {
        Shell.ShellCommand command = Shell.runAsRoot(cmd);
        if (command.output != null && command.output.size() <= 0) {
            return true;
        } else {
            return false;
        }
    }
}
